package org.point.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DateConvertUtil;
import org.point.common.util.DoPage;
import org.point.domain.ProductInOut;
import org.point.domain.ProductType;
import org.point.service.ProductInOutService;
import org.point.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.point.common.util.DateConvertUtil;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: ProductInOutController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:39
 *
 */
@Controller
@RequestMapping("/productIO/")
public class ProductInOutController extends BaseController {

	@Autowired
	private ProductInOutService service;

	/**
	 * 
	 * @Title: productInOutList @Description: TODO(查询商品进出分页数据) @param
	 * @return void 返回类型 @throws
	 */
	@RequestMapping(value = "pdIOList.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void productInOutList(@RequestParam(value = "currentPage") Integer currentPage,
			@RequestParam(value = "keyWord", required = false) String keyWord,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "starttime", required = false) String  starttime,
			@RequestParam(value = "endtime", required = false) String endtime, HttpServletResponse response) {
		JSONObject jsonObj = new JSONObject();
		DoPage<ProductInOut> page = new DoPage<ProductInOut>();
		Map<String, Object> map = new HashMap<String, Object>();
		DateConvertUtil date_time = new DateConvertUtil();
		try {

			if (currentPage != null) {
				page.setCurrentPage(currentPage);
			}
			page.setPageSize(10);
			map.put("firstRecord", page.getFirstRecord());
			map.put("pageSize", page.getPageSize());
			if (strOperateUtil.isNullForString(keyWord)) {
				map.put("keyWord", "%" + keyWord + "%");
			}
			if(starttime!=null&& endtime!=null){
				map.put("starttime", starttime);
				map.put("endtime", endtime);
				
			}
			if (strOperateUtil.isNullForString(status)) {
				map.put("status", status);
			}
			page.setMap(map);
			// 查询商品分类数据分页返回数据
			DoPage<ProductInOut> resultPage = service.searchPIOInfo(page);
			// 商品数据分页返回成功及返回状态
			jsonObj.put("page", resultPage);
			jsonObj.put("resultMsg", 1);
		} catch (Exception e) {
			e.printStackTrace();
			// 网络错误，请联系管理员！
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("商品查询出错!", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (IOException e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: insertProductInOut @Description: TODO(新增商品分类) @param @param
	 *         record 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "insertPIO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertProductInOut(@ModelAttribute("ProductInOut") ProductInOut record, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Integer result = 0;

		try {
			if (record != null) {
				// 设置默认测试数据
				String pio_code = getNextNo("PIO");
				record.setCreateOper("Admin");
				record.setCreateDt(new Date());
				record.setDelFlag(new Byte("0"));
				result = service.insertPIO(record, pio_code);
				// 商品分类新增成功！
				jsonObj.put("resultMsg", result);
			} else {
				// 商品分类参数为null,请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			// 网络错误，请联系管理员！
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("商品分类新增错误！");
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: editProForStatus @Description: TODO(批量删除商品进出分类) @param @param
	 *         memberId @param @param status 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "delPIO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delProductInOut(@RequestParam(value = "ids") String ids, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		String newIDs = ids.substring(9, ids.length() - 1);
		System.out.println(newIDs + "****");

		try {
			if (ids != null) {

				map.put("newIDs", newIDs);
				map.put("memberId", 1);
				result = service.deletePIOs(map);
			}
			if (result > 0) {
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: getPrT @Description: TODO(根据商品分类ID，查询详细信息) @param @param
	 *         productTypeId 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "getPIO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getProductInOut(@RequestParam(value = "id") String id, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;

		try {
			if (strOperateUtil.isNullForString(id)) {
				map.put("id", Integer.parseInt(id));
				ProductInOut record = service.searchForId(map);
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询积分规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/*
	 * 获取商品信息
	 */

	@RequestMapping(value = "PropenWindows.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void openWindows(HttpServletResponse response) {
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		try {
			List<Hashtable<Object, Object>> products = service.getProducts();
			if (products != null) {
				jsonObj.put("products", products);
				// 查询兑换规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 兑换规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询兑换规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

}
